home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / SDKs / PCI Driver Development Kit / • Tools / Utility / LogLibrary 950622 / Src / DumpLogMain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-20  |  11.2 KB  |  545 lines  |  [TEXT/MPCC]

  1. /*                                    DumpLogMain.c                                    */
  2. #define EXTERN /* */
  3. #include "DumpLog.h"
  4.  
  5. #ifndef FALSE
  6. #define FALSE    0
  7. #define TRUE    1
  8. #endif
  9. #if MPW
  10. QDGlobals                    qd;
  11. #endif
  12.  
  13. enum AppleMenu {
  14.     kAppleAbout                = 1
  15. };
  16. enum FileMenu {
  17.     kFileQuit                = 1
  18. };
  19. enum EditMenu {
  20.     kEditUndo                = 1,
  21.     kEditUnused,
  22.     kEditCut,
  23.     kEditCopy,
  24.     kEditPaste,
  25.     kEditClear
  26. };
  27.  
  28. void                        EventLoop(void);
  29. void                        InitializeMenus(void);
  30. void                        DoMouseEvent(void);
  31. void                        DoCommand(
  32.         WindowPtr                theWindow,
  33.         long                    menuChoice
  34.     );
  35. void                        AdjustMenus(void);
  36. void                        AdjustEditMenu(
  37.         Boolean                    isDeskAcc
  38.     );
  39. void                        MakeLogFileName(
  40.         StringPtr                logFileName
  41.     );
  42. void                        CreateLogFile(
  43.         ConstStr255Param        logFileName
  44.     );
  45. OSErr                        CreateOutputFile(
  46.         OSType                    creator,
  47.         ConstStr255Param        fileName,
  48.         short                    vRefNum,
  49.         short                    *fileRefNum
  50.     );
  51. void                        WriteOutputLine(
  52.         ConstStr255Param        datum,
  53.         short                    fileRefNum
  54.     );
  55. void                        WriteNewline(
  56.         short                    fileRefNum
  57.     );
  58. void                        CloseOutputFile(
  59.         OSErr                    status,
  60.         short                    fileRefNum,
  61.         short                    volumeRefNum,
  62.         ConstStr255Param        fileName
  63.     );
  64.  
  65. WindowPtr                    MakeStatusWindow(void);
  66. void                        UpdateStatusWindow(
  67.         WindowPtr                statusWindowPtr
  68.     );
  69. void                        ShowStatusString(
  70.         WindowPtr                statusWindowPtr,
  71.         ConstStr255Param        theText
  72.     );
  73. WindowPtr                    gStatusWindow;
  74. #define    IsOurWindow(theWindow)    ((theWindow) == gStatusWindow)
  75. Str255                        gLogFileName;
  76.  
  77. #if TESTING
  78. LogRecordPtr                gLogRecordPtr;
  79. #endif
  80.  
  81. void
  82. main(void)
  83. {
  84.         MaxApplZone();        
  85.         InitGraf(&qd.thePort);
  86.         InitFonts();
  87.         InitWindows();
  88.         InitMenus();
  89.         TEInit();
  90.         InitDialogs(0);
  91.         InitializeMenus();
  92.         /* */
  93.         MakeLogFileName(gLogFileName);
  94.         CreateLogFile(gLogFileName);
  95.         /* */
  96.         gStatusWindow = MakeStatusWindow();
  97.         if (gStatusWindow == NULL)
  98.             DebugStr("\pDisplay window failed");
  99.         else {
  100.             SetWTitle(gStatusWindow, gLogFileName);
  101.             if (1) { /*  Debug: create a log using gLogFileName    */
  102.                 LogRecordPtr        testLogPtr;
  103.  
  104.                 gLogFileName[gLogFileName[0]+1] = '\0';
  105.                 testLogPtr = MakeLogRecord((char *) &gLogFileName[1], 10);
  106.                 LogString(testLogPtr, 'Test', gLogFileName);
  107.             }
  108.             DoAllLogRecords();
  109.             while (gQuitNow == FALSE)
  110.                 EventLoop();
  111.         }
  112.         CloseOutputFile(noErr, gFileRefNum, gVolRefNum, gFileName);
  113. }
  114.  
  115. /*
  116.  * Application event loop: process one event each time.
  117.  */
  118. void
  119. EventLoop(void)
  120. {
  121.         long                    menuChoice;
  122.         register WindowPtr        theWindow;
  123.         GrafPtr                    savePort;
  124.         Boolean                    isActivating;
  125.         char                    theChar;
  126.                 
  127.         if (gUpdateMenusNeeded)
  128.             AdjustMenus();
  129.         WaitNextEvent(
  130.             everyEvent,
  131.             &EVENT,
  132.             (gInForeground) ? 6L : 60L,
  133.             NULL
  134.         );
  135.         theWindow = FrontWindow();
  136.         switch (EVENT.what) {
  137.         case nullEvent:
  138.             WriteAllLogRecords();
  139.             break;
  140.         case keyDown:
  141.         case autoKey:
  142.             if ((EVENT.message & charCodeMask) == '.'
  143.              && (EVENT.modifiers & cmdKey) != 0) {
  144.                 FlushEvents(keyDown | autoKey, 0);
  145.                 gQuitNow = TRUE;
  146.             }
  147.             else if ((EVENT.modifiers & cmdKey) != 0) {
  148.                 if (EVENT.what == keyDown) {
  149.                     menuChoice = MenuKey(EVENT.message & charCodeMask);
  150.                     if (HiWord(menuChoice) != 0 && IsOurWindow(theWindow))
  151.                         DoCommand(theWindow, menuChoice);
  152.                     else if (IsOurWindow(theWindow))
  153.                         goto doKeyDown;
  154.                     else {
  155.                         SysBeep(10);
  156.                     }
  157.                 }
  158.             }
  159.             else if (IsOurWindow(theWindow)) {
  160. doKeyDown:            theChar = EVENT.message & charCodeMask;
  161.                 if (theChar != '\015')
  162.                     gMessageString[++gMessageString[0]] = theChar;
  163.                 else {
  164.                     ShowStatusString(gStatusWindow, gMessageString);
  165.                 }
  166.             }
  167.             else {
  168.                 SysBeep(10);
  169.             }
  170.             break;
  171.         case mouseDown:
  172.             DoMouseEvent();
  173.             break;
  174.         case updateEvt:
  175.             theWindow = (WindowPtr) EVENT.message;
  176.             GetPort(&savePort);
  177.             SetPort(theWindow);
  178.             BeginUpdate(theWindow);
  179.             EraseRect(&theWindow->portRect);
  180.             if (IsOurWindow(theWindow))
  181.                 UpdateStatusWindow(theWindow);
  182.             else {
  183.                 DrawControls(theWindow);
  184.                 DrawGrowIcon(theWindow);
  185.             }
  186.             EndUpdate(theWindow);
  187.             SetPort(savePort);
  188.             break;
  189.         case activateEvt:
  190.             theWindow = (WindowPtr) EVENT.message;
  191.             isActivating = ((EVENT.modifiers & activeFlag) != 0);
  192.             goto activateEvent;
  193.             break;
  194.         case osEvt:
  195.             switch (((unsigned long) EVENT.message) >> 24) {
  196.             case mouseMovedMessage:
  197.                 break;
  198.             case suspendResumeMessage:
  199.                 isActivating = ((EVENT.message & 0x01) != 0);
  200. activateEvent:            if (isActivating) {
  201.                     /*
  202.                      * Activate this window. Activate events define theWindow from
  203.                      * the event record, while suspend/resume uses the pre-set
  204.                      * FrontWindow value.
  205.                      */
  206.                     SelectWindow(theWindow);
  207.                     (void) TEFromScrap();
  208.                 }
  209.                 if (IsOurWindow(theWindow)) {
  210.                     /*
  211.                      * Nothing special
  212.                      */
  213.                 }
  214.                 else {
  215.                     /* Desk accessory or what? */
  216.                 }
  217.                 gInForeground = isActivating;
  218.                 gUpdateMenusNeeded = TRUE;
  219.                 break;
  220.             }
  221.             break;
  222.         }
  223. }
  224.  
  225. /*
  226.  * DoMouseEvent
  227.  * The user clicked on something. Handle application-wide processing here, or call
  228.  * a Test function for specific action.
  229.  */
  230. void
  231. DoMouseEvent(void)
  232. {
  233.         WindowPtr                theWindow;
  234.         short                    whichPart;
  235.         
  236.         whichPart = FindWindow(EVENT.where, &theWindow);
  237.         if (theWindow == NULL)
  238.             theWindow = FrontWindow();
  239.         if (whichPart == inMenuBar && IsOurWindow(theWindow) == FALSE)
  240.             theWindow = FrontWindow();
  241.         switch (whichPart) {
  242.         case inDesk:
  243.             break;
  244.         case inMenuBar:
  245.             InitCursor();
  246.             DoCommand(theWindow, MenuSelect(EVENT.where));
  247.             break;
  248.         case inDrag:
  249.             DragWindow(theWindow, EVENT.where, &qd.screenBits.bounds);
  250.             break;
  251.         case inGoAway:
  252.             if (TrackGoAway(theWindow, EVENT.where)) {
  253.                 if (IsOurWindow(theWindow))
  254.                     gQuitNow = TRUE;
  255.                 else {
  256.                     SysBeep(10);
  257.                 }
  258.             }
  259.             break;
  260.         case inContent:
  261.             if (theWindow != FrontWindow())
  262.                 SelectWindow(theWindow);
  263.             else if (IsOurWindow(theWindow)) {
  264.                 /* Nothing happens here */;
  265.             }
  266.             else {
  267.                 /* Nothing happens here        */
  268.             }
  269.             break;
  270.         default:
  271.             break;                            /* Bogus click: ignore                */
  272.         }
  273. }
  274.  
  275. /*
  276.  * DoCommand
  277.  * Process a menu or keystroke command.
  278.  */
  279. void
  280. DoCommand(
  281.         WindowPtr                theWindow,
  282.         long                    menuChoice
  283.     )
  284. {
  285.         short                    menuItem;
  286.         Str255                    menuText;
  287.         GrafPtr                    savePort;
  288.         
  289.         menuItem = LoWord(menuChoice);
  290.         switch (HiWord(menuChoice)) {
  291.         case MENU_Apple:
  292.             if (menuItem == kAppleAbout)
  293.                 ; /* Nothing happens here */
  294.             else {
  295.                 GetMenuItemText(gAppleMenu, menuItem, menuText);
  296.                 AdjustEditMenu(TRUE);
  297.                 GetPort(&savePort);
  298.                 OpenDeskAcc(menuText);
  299.                 SetPort(savePort);
  300.                 AdjustEditMenu(IsOurWindow(theWindow) == FALSE);
  301.             }
  302.             break;
  303.         case MENU_File:
  304.             switch (menuItem) {
  305.             case kFileQuit:
  306.                 gQuitNow = TRUE;
  307.                 break;
  308.             }
  309.             break;
  310.         case MENU_Edit:
  311.             if (SystemEdit(menuItem - 1) == FALSE)
  312.                 SysBeep(10);
  313.             break;
  314.         }
  315.         HiliteMenu(0);
  316. }        
  317.  
  318. /*
  319.  * AdjustMenus
  320.  * Enable/disable menu options.
  321.  */
  322. void
  323. AdjustMenus(void)
  324. {
  325.         EnableItem(gFileMenu, kFileQuit);
  326.         AdjustEditMenu(IsOurWindow(FrontWindow()) == FALSE);
  327. }
  328.  
  329. /*
  330.  * AdjustEditMenu
  331.  * Enable/disable Edit Menu options.
  332.  */
  333. void
  334. AdjustEditMenu(
  335.         Boolean                    isDeskAcc
  336.     )
  337. {
  338.         if (isDeskAcc) {
  339.             EnableItem(gEditMenu, kEditUndo);
  340.             EnableItem(gEditMenu, kEditCut);
  341.             EnableItem(gEditMenu, kEditCopy);
  342.             EnableItem(gEditMenu, kEditPaste);
  343.             EnableItem(gEditMenu, kEditClear);
  344.         }
  345.         else {
  346.             DisableItem(gEditMenu, kEditUndo);
  347.             DisableItem(gEditMenu, kEditCut);
  348.             DisableItem(gEditMenu, kEditCopy);
  349.             DisableItem(gEditMenu, kEditPaste);
  350.             DisableItem(gEditMenu, kEditClear);
  351.         }
  352. }
  353.  
  354. void
  355. InitializeMenus(void)
  356. {
  357.         gAppleMenu = NewMenu(MENU_Apple, "\p\024");
  358.         AppendMenu(gAppleMenu, "\p(No About;(-");
  359.         AppendResMenu(gAppleMenu, 'DRVR');
  360.         InsertMenu(gAppleMenu, 0);
  361.         gFileMenu = NewMenu(MENU_File, "\pFile");
  362.         AppendMenu(gFileMenu, "\pQuit/Q");
  363.         InsertMenu(gFileMenu, 0);
  364.         gEditMenu = NewMenu(MENU_Edit, "\pEdit");
  365.         AppendMenu(gEditMenu, "\pUndo/Z;(-;Cut/X;Copy/C;Paste/P;Clear");
  366.         InsertMenu(gEditMenu, 0);
  367.         DrawMenuBar();
  368.         InitCursor();
  369. }
  370.  
  371.  
  372. void
  373. DumpDrawLine(
  374.         ConstStr255Param        datum
  375.     )
  376. {
  377.         pstrcat(gCurrentLine, datum);
  378.         WriteOutputLine(gCurrentLine, gFileRefNum);
  379.         gCurrentLine[0] = 0;
  380. }
  381.  
  382. void
  383. DumpDrawString(
  384.         ConstStr255Param        datum
  385.     )
  386. {
  387.         pstrcat(gCurrentLine, datum);
  388. }
  389.  
  390. void
  391. DumpDrawText(
  392.         const char                *datum,
  393.         unsigned short            length
  394.     )
  395. {
  396.         short                    len;
  397.         
  398.         len = 255 - gCurrentLine[0];
  399.         if (len > length)
  400.             len = length;
  401.         BlockMove(datum, &gCurrentLine[1] + gCurrentLine[0], len);
  402.         gCurrentLine[0] += len;
  403. }
  404.  
  405. void
  406. MakeLogFileName(
  407.         StringPtr                logFileName
  408.     )
  409. {
  410.         unsigned long            now;
  411.         Intl0Hndl                dateFormat;
  412.         Str255                    timeString;
  413.  
  414.         GetDateTime(&now);
  415.         dateFormat = (Intl0Hndl) GetIntlResource(1);
  416.         (**dateFormat).dateOrder = ymd;                /* Year month day            */
  417.         (**dateFormat).shrtDateFmt = (dayLdingZ | mntLdingZ | century);
  418.         (**dateFormat).dateSep = '.';
  419.         (**dateFormat).timeCycle = timeCycle24;
  420.         (**dateFormat).timeFmt = (secLeadingZ | minLeadingZ | hrLeadingZ);
  421.         (**dateFormat).timeSep = 0;                    /* No separator                */
  422.         IUDatePString(now, shortDate, logFileName, (Handle) dateFormat);
  423.         pstrcat(logFileName, "\p ");
  424.         IUTimePString(now, FALSE, timeString, (Handle) dateFormat);
  425.         pstrcat(logFileName, timeString);
  426. }
  427.  
  428. void
  429. CreateLogFile(
  430.         ConstStr255Param        logFileName
  431.     )
  432. {
  433.         OSErr                    status;
  434.         
  435.         status = GetVol(NULL, &gVolRefNum);
  436.         if (status != noErr)
  437.             gVolRefNum = 0;
  438.         (void) CreateOutputFile(kOutputCreatorID, logFileName, gVolRefNum, &gFileRefNum);
  439. }
  440.         
  441. OSErr
  442. CreateOutputFile(
  443.         OSType                    creator,
  444.         ConstStr255Param        fileName,
  445.         short                    vRefNum,
  446.         short                    *fileRefNum
  447.     )
  448. {
  449.         OSErr                    status;
  450.         
  451.         /*
  452.          * Create the file, elmininating any duplicate.
  453.          */
  454.         SetCursor(*GetCursor(watchCursor));
  455.         BlockMove(fileName, gFileName, fileName[0] + 1);
  456.         status = Create(fileName, vRefNum, creator, 'TEXT');
  457.         if (status == dupFNErr)     {                    /* Exists already?        */
  458.             status = FSDelete(fileName, vRefNum);
  459.             if (status == noErr)
  460.                 status = Create(fileName, vRefNum, creator, 'TEXT');
  461.         }
  462.         if (status == noErr)
  463.             status = FSOpen(fileName, vRefNum, fileRefNum);
  464.         if (status != noErr)
  465.             *fileRefNum = 0;
  466.         InitCursor();
  467.         return (status);
  468. }
  469.  
  470.  
  471. #define kEndOfLine        0x0D                    /* Return            */
  472. static char                gEndOfLine[1] =            { kEndOfLine };
  473.  
  474. /*
  475.  * Write one line of text to the output file. Ignore errors.
  476.  */
  477. void
  478. WriteOutputLine(
  479.         ConstStr255Param        datum,
  480.         short                    fileRefNum
  481.     )
  482. {
  483.         long                    length;
  484.         
  485.         if (datum[0] > 0) {
  486.             length = datum[0];
  487.             (void) FSWrite(fileRefNum, &length, (Ptr) &datum[1]);
  488. #if 0
  489.             {
  490.                 short                    i;
  491.                 Str255                    work;
  492.  
  493.                 /*
  494.                  * We need to hack the string to force 7-bit Ascii
  495.                  * for the Comm Toolbox windows.
  496.                  */
  497.                 BlockMove(datum, work, datum[0] + 1);
  498.                 for (i = 1; i <= work[0]; i++) {
  499.                     switch (work[i]) {
  500.                     case 0xCA:    work[i] = ' ';    break;
  501.                     case 0xA5:    work[i] = '*';    break;
  502.                     case 0xD2:    work[i] = '"';    break;
  503.                     case 0xD3:    work[i] = '"';    break;
  504.                     case 0xD4:    work[i] = '\'';    break;
  505.                     case 0xD5:    work[i] = '\'';    break;
  506.                     default:                    break;
  507.                     }
  508.                 }
  509.                 ShowStatusString(gStatusWindow, work);
  510.             }
  511. #else
  512.             ShowStatusString(gStatusWindow, datum);
  513. #endif
  514.         }
  515.         WriteNewline(fileRefNum);
  516. }
  517.  
  518. void
  519. WriteNewline(
  520.         short                fileRefNum
  521.     )
  522. {
  523.         long                    length;
  524.         
  525.         length = 1;
  526.         (void) FSWrite(fileRefNum, &length, (Ptr) gEndOfLine);
  527. }
  528.  
  529. void
  530. CloseOutputFile(
  531.         OSErr                    status,
  532.         short                    fileRefNum,
  533.         short                    volumeRefNum,
  534.         ConstStr255Param        fileName
  535.     )
  536. {
  537.         (void) FSClose(fileRefNum);
  538.         (void) FlushVol(NULL, volumeRefNum);
  539.         if (status != noErr)
  540.             (void) FSDelete(fileName, volumeRefNum);
  541. }
  542.  
  543.  
  544.  
  545.